(function($) {
	var datagrid = '#dg_keyword', toolbar = '#toolbars_keyword', add = '#add_keyword', del = '#delete_keyword', update = '#update_keyword', reload = '#reload_keyword',
		form = '#form_keyword', submit = '#submit', closeWin = '#closeWin', search = '#search_keyword',
		templateType = '#templateType', templateId = '#templateId';
	
	var keywordObj = function() {
		var self = this ;
		this.dg = null ;
		this.url_form = window.app.domain.URL_ADMIN_UI + '/pages/weixin/keyword/form.html' ;
		this.url_list = window.app.domain.URL_API_WEIXIN_REST + '/weixin/keyword/list' ;
		this.url_add = window.app.domain.URL_API_WEIXIN_REST + '/weixin/keyword/add' ;
		this.url_update = window.app.domain.URL_API_WEIXIN_REST + '/weixin/keyword/update' ;
		this.url_delete = window.app.domain.URL_API_WEIXIN_REST + '/weixin/keyword/delete' ;
		this.url_get = window.app.domain.URL_API_WEIXIN_REST + '/weixin/keyword/get' ;
		
		this.initDatagrid() ;
		this.initBindBtnEvent();
		
		//搜索
		$(search).searchbox({
			menu:'#mm_keyword',
			searcher:function(value,name){
				var params = {} ;
				params[name] = value ;
				self.dg.datagrid('load', params);
		    }
		});
	};
	
	keywordObj.prototype.initDatagrid = function() {
		var self = this ;
		this.dg = $(datagrid).datagrid({
			url: self.url_list, 
			method: 'GET', nowrap: true, striped: true, singleSelect: false,
			checkbox: true, pagination: true, pageSize: 30, pageList: [30,40,50,100,200,500,1000],
			autoRowHeight: true, fit: true, border: false, rownumbers: true, rownumberWidth: 40, toolbar: toolbar, 
			remoteSort: true, sortName: 'id', sortOrder: 'desc', queryParams: {},
			frozenColumns: [[
			           {title: 'ID', field: 'id', hidden: true},
			           {field: 'ck', checkbox:true}
			]],
			columns: [[
			           {title: '关键字', field: 'keyword', width: 200},
			           {title: '模板类型', field: 'templateType', width: 150, formatter: function(value, row, index) {
			        	   	if(value == "text") {return "<span>文本消息</span>";}
			   				else if(value == "article_single") {return "<span>单图文消息</span>";}
			   				else if(value == "article_group") {return "<span>多图文消息</span>";}
			           }},
			           {title: '状态', field: 'status', width: 80, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   if(value == 1) return '<span class="label label-success">启用</span>';
			        	   else return '<span class="label label-danger">禁用</span>';
			           }},
			           {title: '参数', field: 'params', width: 200},
			           {title: '访问次数', field: 'accessCount', width: 100},
			           {title: '创建时间', field: 'createTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			           {title: '更新时间', field: 'updateTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			]],
			onLoadSuccess: function(data) {
			},
			onLoadError: function() {
			}
		});
	};
	
	keywordObj.prototype.initBindBtnEvent = function() {
		var self = this ;
		$(add).on('click', function() { self.add() });
		$(del).on('click', function() { self.del() });
		$(update).on('click', function() { self.update() });
		$(reload).on('click', function() { self.reload() });
	};
	
	keywordObj.prototype.add = function() {
		var self = this ;
		window.app.easyui.addWindow({title: '添加', href: self.url_form, width: 600, height: 320,
			onLoad: function() {
				//模板ID
				$(templateId).combobox({valueField: 'id', textField: 'title', editable: false, panelHeight:'auto', width: 400});
				
				//模板类型
				$(templateType).combobox({
					onSelect: function(rec) {
						$(templateId).combobox('setValue', null);
						self.showTemplateList(rec.value) ;
					}
				});
				
				$(submit).on('click', function() {self.submitForm(self.url_add);});
			}
		});
	};
	
	keywordObj.prototype.showTemplateList = function(type, selected) {
		var url = window.app.domain.URL_API_WEIXIN_REST + '/weixin/text/combobox' ;
		if(type === "article_single") {
			url = window.app.domain.URL_API_WEIXIN_REST + '/weixin/article/combobox';
		}
		if(type === "article_group") {
			url = window.app.domain.URL_API_WEIXIN_REST + '/weixin/article_group/combobox';
		}
		$.ajax({
			url: url, type: "GET",
			success: function(result) {
				$(templateId).combobox('loadData', result);
			},
			error: function(error) {
			}
		});
	};
	
	keywordObj.prototype.update = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			if(selections.length == 1) {
				window.app.easyui.addWindow({title: '修改', href: self.url_form, width: 600, height: 320,
					onLoad: function() {
						
						$.get(self.url_get + '/' + selections[0], function(result) {
							$(form).form('load', result);
							self.showTemplateList(result.templateType, result.templateId) ;
							//模板类型
							$(templateType).combobox({
								value: result.templateType,
								onChange: function(newValue, oldValue) {
									self.showTemplateList(newValue) ;
									$(templateId).combobox('setValue', null);
								}
							});
						})
						
						
						$(submit).on('click', function() {
							self.submitForm(self.url_update);
						});
					}
				});
			} else {
				alertify.error('只能选择一条记录！');
			}
		} else {
			alertify.error('请选择记录！');
		}
		
	};
	
	keywordObj.prototype.del = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			$.messager.confirm('删除确认', '确定要删除？', function(r) {
				if(r) {
					$.ajax({
						type: 'GET',
						url: self.url_delete + '/' + selections.join(','),
						beforeSend: function() {
							$.easyui.loading();
						},
						success: function(result) {
							try {
								if(result.status) {
									self.reload(); alertify.success(result.message);
								} else {
									alertify.warning(result.message);
								}
							} catch(e) {
								$.easyui.loaded(); console.error(e) ;
							}
						},
						complete: function(xhr, textStatus) {
							$.easyui.loaded();
						}
					});
				}
			});
		} else {
			alertify.error('至少选择一条记录');
		}
	};
	
	keywordObj.prototype.get = function(id) {
		var self = this ;
		$.ajax({
			type: 'GET',
			url: self.url_get + id,
			beforeSend: function() {
				$.easyui.loading();
			},
			success: function(result) {
				try {
					return result ;
				} catch(e) {
					$.easyui.loaded(); console.error(e) ;
				}
			},
			complete: function(xhr, textStatus) {
				$.easyui.loaded();
			}
		});
	};
	
	keywordObj.prototype.submitForm = function(url) {
		var self = this ;
		var checkform = $(form).form('validate');
		if(checkform) {
			$.ajax({
				type: 'POST',
				url: url,
				data: window.app.util.getData(form),
				beforeSend: function() {
					$.easyui.loading();
				},
				success: function(result) {
					try {
						if(result.status) {
							self.reload(); window.app.easyui.closeCurrentWindow(); alertify.success(result.message);
						} else {
							alertify.warning(result.message);
						}
					} catch(e) {
						$.easyui.loaded(); console.error(e) ;
					}
				},
				complete: function(xhr, textStatus) {
					$.easyui.loaded();
				}
			});
		}
	};
	
	keywordObj.prototype.reload = function() {
		this.dg.datagrid('reload'); this.dg.datagrid('clearSelections');this.dg.datagrid('clearChecked');
	};
	
	var keyword = new keywordObj() ;
})(jQuery);
	
